package com.example.hospitalapp;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class PaymentActivity extends AppCompatActivity {

    private Button btnConfirmPayment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment);

        btnConfirmPayment = findViewById(R.id.btnConfirmPayment);

        // 获取传递过来的预约ID
        int appointmentId = getIntent().getIntExtra("appointmentId", 0);
        Log.d("PaymentActivity", "接收到的 appointmentId: " + appointmentId);

        btnConfirmPayment.setOnClickListener(view -> {
            // 确认支付逻辑
            confirmPayment(appointmentId);
            addToQueue(appointmentId);
        });
    }

    private void confirmPayment(int appointmentId) {
        // 更新支付状态为已缴费
        JSONObject params = new JSONObject();
        try {
            params.put("appointmentId", appointmentId);
            params.put("paymentStatus", "已缴费");

            ApiClient apiClient = new ApiClient(PaymentActivity.this);
            apiClient.putRequest("http://zhsj.autumnclouds.cn/appointment", params, new ApiClient.VolleyCallback() {
                @Override
                public void onSuccess(JSONObject response) {
                    // 处理支付逻辑
                    Log.d("PaymentActivity", "支付请求成功：" + response.toString());
                }
                @Override
                public void onFailure(String error) {
                    Log.d("PaymentActivity", "支付请求失败：");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void addToQueue(int appointmentId) {
        // 生成随机排队号（3到10之间）
        Random random = new Random();
        int queueNumber = random.nextInt(8) + 3;  // 生成 3~10 的随机数

        // 获取当前时间作为排队时间
        String queueTime = getCurrentTime();

        JSONObject params = new JSONObject();
        try {
            params.put("appointmentId", appointmentId);
            params.put("queueNumber", queueNumber);
            params.put("queueTime", queueTime);
            params.put("calledStatus", "未叫号");

            // 向排队接口提交数据
            ApiClient apiClient = new ApiClient(PaymentActivity.this);
            apiClient.postRequest("http://zhsj.autumnclouds.cn/queue", params, new ApiClient.VolleyCallback() {
                @Override
                public void onSuccess(JSONObject response) {
                    try {
                        int code = response.getInt("code");
                        if (code == 200) {
                            // 排队成功，显示提示
                            Toast.makeText(PaymentActivity.this, "缴费成功，已加入排队队列", Toast.LENGTH_SHORT).show();
                            finish();  // 返回预约页面
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                @Override
                public void onFailure(String error) {
                    Toast.makeText(PaymentActivity.this, "缴费失败", Toast.LENGTH_SHORT).show();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getCurrentTime() {
        // 获取当前时间，作为排队时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdf.format(new Date());
    }
}

